/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.util.logging.Level;
import java.util.logging.Logger;
import proxy.*;
import java.util.ArrayList;
import java.util.Vector;

/**
 *
 * @author root
 */
public class EscritorSocket implements Runnable {

    private ISocketPeer peer;
    private boolean run = true;

    public EscritorSocket(ISocketPeer peer) {
        this.peer = peer;
    }
    private ArrayList<String> linhas = new ArrayList<String>();

    public synchronized void escreverLinha(String linha) {
        linhas.add((linha + "\r\n"));
        this.notify();
    }

    private synchronized void enviaLinhas() {
        if (peer.getSocket().isConnected()) {
            try {
                for (String l : linhas) {
                    peer.getBuffWriter().write(l);
                    peer.getBuffWriter().flush();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            linhas.clear();
        }
    }

    public void run() {
        while (run) {
            if (peer != null) {
                enviaLinhas();
            }
            esperar();
        }

    }

    private synchronized void esperar() {
        try {
            wait();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
